package com.www;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI_9 {
    private static void createAndShowGUI(){
        JFrame f = new JFrame("keyEvent");
        f.setLayout(new FlowLayout());
        f.setSize(400,300);
        f.setLocation(300,200);
        JTextField tf = new JTextField(30);
        f.add(tf);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tf.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e){
                char keyChar = e.getKeyChar();
                int keyCode = e.getKeyCode();
                System.out.println("键盘按下的字符内容为："+keyChar+" ");
                System.out.println("键盘按下的字符代码为："+keyCode);
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(GUI_9::createAndShowGUI);
    }
}
